package com.example.isap_wtf.Model

import android.R
import android.app.Activity
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment


class MyDialogFragment: DialogFragment() {
    private var mListener: MyDialogListener? = null

    fun newInstance(): MyDialogFragment? {
        val title = "Важно!"
        val myDialogFragment = MyDialogFragment()
        val args = Bundle()
        args.putString("title", title)
        myDialogFragment.setArguments(args)
        return myDialogFragment
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val title: String? = getArguments()!!.getString("title")
        val builder: android.app.AlertDialog.Builder = android.app.AlertDialog.Builder(getActivity())
        builder.setMessage("Вы жертвуете миллион коту")
            .setTitle(title)
            .setPositiveButton("OK",
                DialogInterface.OnClickListener { dialog, id ->
                    mListener!!.onDialogPositiveClick(
                        this@MyDialogFragment
                    )
                })
            .setNegativeButton("Cancel",
                DialogInterface.OnClickListener { dialog, id ->
                    mListener!!.onDialogNegativeClick(this@MyDialogFragment)
                })
        return builder.create()
    }

    override fun onAttach(activity: Activity) {
        super.onAttach(activity)
        mListener = try {
            activity as MyDialogListener
        } catch (e: ClassCastException) { // The activity doesn't implement the interface, throw exception
            throw ClassCastException(
                activity.toString()
                        + " must implement NoticeDialogListener"
            )
        }
    }

    interface MyDialogListener {
        fun onDialogPositiveClick(dialog: DialogFragment?)
        fun onDialogNegativeClick(dialog: DialogFragment?)
    }
}